package de.zainodis.authorlist.ui;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import de.zainodis.authorlist.R;
import de.zainodis.authorlist.core.Application;
import de.zainodis.authorlist.model.AuthorLibraryComponent;
import de.zainodis.authorlist.model.parsing.AuthorParser;
import de.zainodis.commons.utils.Toaster;

/**
 * Displays a list of authors the user can chose from.
 * 
 * @author fzarrai
 * 
 */
public class AuthorListFragment extends ListDownloadBaseFragment {

   public static final String TAG = "AuthorListFragment";

   private ArrayAdapter<String> listAdapter;

   @Override
   public void onActivityCreated(Bundle savedInstanceState) {
	 super.onActivityCreated(savedInstanceState);

	 AuthorLibraryComponent library = Application.getComponent(AuthorLibraryComponent.class);

	 listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
		  new ArrayList<String>(library.getAuthors()));
	 listAdapter.setNotifyOnChange(true);
	 setListAdapter(listAdapter);
   }

   @Override
   public void onResume() {
	 super.onResume();
	 AuthorLibraryComponent library = Application.getComponent(AuthorLibraryComponent.class);
	 if (!library.hasAuthors()) {
	    if (internetAvailable(true)) {
		  // Hide the list until we've loaded it
		  setListShown(false);
		  AuthorsDownloadTask task = new AuthorsDownloadTask();
		  task.execute();
	    }
	 }
   }

   @Override
   public void onListItemClick(ListView l, View v, int position, long id) {
	 String author = listAdapter.getItem(position);
	 showDetails(author);
   }

   protected void showDetails(String author) {
	 String fragmentName = AuthorsCreationListFragment.class.getName();
	 Bundle bundle = new Bundle();
	 bundle.putString(AuthorsCreationListFragment.AUTHOR_NAME, author);
	 Fragment fragment = Fragment.instantiate(getActivity(), fragmentName, bundle);

	 FragmentTransaction transaction = getFragmentManager().beginTransaction();
	 transaction.replace(android.R.id.content, fragment, AuthorListFragment.TAG);
	 transaction.addToBackStack(null);
	 transaction.commit();
   }

   private class AuthorsDownloadTask extends XmlDownloadTask<String> {

	 @Override
	 protected void parse(InputStream stream) throws XmlPullParserException, IOException {
	    new AuthorParser().parse(stream);
	 }

	 @Override
	 protected void onPostExecute(Void result) {
	    AuthorLibraryComponent library = Application.getComponent(AuthorLibraryComponent.class);
	    Collection<String> authors = library.getAuthors();
	    listAdapter.addAll(authors);
	    listAdapter.notifyDataSetChanged();
	    // We're done loading items, show the list
	    setListShown(true);
	 }

	 @Override
	 protected String getUrl() {
	    return Application.getStringFromResId(R.string.authors_url);
	 }

	 @Override
	 protected String getTag() {
	    return "AuthorsDownloadTask";
	 }
   }
}
